package com.timothy.android;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.timothy.android.view.BaseBean;
import com.timothy.answer.ChoiceResult;
import com.timothy.chart.BaseChart;
import com.timothy.service.MessageReceiveService;
import com.timothy.statistics.ButtonStatistics;
import com.timothy.statistics.GradingStatistics;
import com.timothy.statistics.PriorityStatistics;
import com.timothy.statistics.StatisticsBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;

public class BaseAnswerActivity extends BaseProcessActivity implements OnDoubleTapListener, OnGestureListener,OnTouchListener{
	private final static String LOG_TAG = "BaseAnswerActivity";
	
	protected final static Integer QUESTION_WRAP_LENGTH_VERTIAL = 35;
	protected final static Integer QUESTION_WRAP_LENGTH_HORIZONTAL = 65;
	
	protected ButtonStatistics answer = new ButtonStatistics();
	protected BaseBean baseBean = new BaseBean();
	protected String dataXML = null;
	
	//answer chart list
	protected List<BaseChart> chartList = new ArrayList<BaseChart>();
	
	protected String[] chartColorArr = new String[]{"#ff0000","#ff7f00","#ffff00","#56ff56","#0fff0f","#007fff","#7f00ff","#ff007f"};
	protected String[] shadowColorArr = new String[]{"#bf0000","#bf5f00","#bfbf00","#5fbf00","#007f7f","#003f7f","#3f007f","#7f003f"};
	protected int textSize = 18;
	protected String questionTextColor = "#ffffff";
	protected final static int questionTextSize = 15;
	protected final static String choiceTextColor = "#000000";
	protected final static int choiceTextSize = 15;
	protected final static String answerTextColor = "#000000";
	protected final static int answerTextSize = 15;
	
	//question type
	protected String type = null;
	protected GestureDetector mGestureDetector = null;
	
	public void initialData(String dataXML){
		Log.i(LOG_TAG,"------------start initialData-------------");
		Log.i(LOG_TAG,"parse dataXML:"+dataXML);
		
		answer = StatisticsBuilder.parseSTTwoButtonAlphaAnswer(dataXML);
		Log.i(LOG_TAG,"answer message:"+answer.toMessage());
		
		List<ChoiceResult> choiceResultList = answer.getChoiceResults();
		if(choiceResultList==null||choiceResultList.size()==0){
			return;
		}
		
		String appendTitle = "(SlideIndex:"+answer.getSlide()+")";
		this.appendTitle(appendTitle);
		Log.i(LOG_TAG,appendTitle);
		
		for(ChoiceResult cr:choiceResultList){
			BaseChart chart = new BaseChart();
			int index = cr.getIndex();
			Log.i(LOG_TAG,"index:"+index);
			chart.setIndex(index);
			chart.setVotes(cr.getCount());
			if(answer.getTotal()>0){
				chart.setShare(cr.getCount()*1.0/answer.getTotal());
			}else{
				chart.setShare(0);
			}
			chart.setChoiceText(String.valueOf(cr.getContent()));
			chart.setFullColor(chartColorArr[index%8]);
			chart.setShadowColor(shadowColorArr[index%8]);
			chart.setChoiceTextSize(textSize);
			chart.setChoiceTextColor(chartColorArr[index%8]);
			chart.setAnswerType(3);
			chart.setAnswerText(3);
			chart.setAnswerTextColor(chartColorArr[index%8]);
			chart.setAnswerTextSize(textSize);
			chartList.add(chart);
		}
		
		
		Log.i(LOG_TAG,"question name:"+answer.getQuestion().getQuestionName());
		
		baseBean.setQuestionText(answer.getQuestion().getQuestionName());
		baseBean.setCrList(answer.getChoiceResults());
		
		Log.i(LOG_TAG,"------------end initialData-------------");
	}
	
	public void initialPriorityData(String dataXML,Integer priority){
		Log.i(LOG_TAG,"------------start initialPriorityData-------------");
		
		statistics = StatisticsBuilder.parsePriorityStatistics(dataXML);
		baseBean.setQuestionText(statistics.getQuestion().getQuestionName());
		
		Map<Integer,List<ChoiceResult>> priorityMap = statistics.getPriorityMap();
		
		List<ChoiceResult> crList;
		if(priorityMap!=null&&priorityMap.size()>0){
			
			prioritySize = priorityMap.size();
			Log.i(LOG_TAG,"prioritySize="+prioritySize);
			
			crList = priorityMap.get(priority);
			Log.i(LOG_TAG,"priority="+priority);
			for(ChoiceResult cr:crList){
				BaseChart chart = new BaseChart();
				int index = cr.getIndex();
				Log.i(LOG_TAG,"index:"+index);
				chart.setIndex(index);
				chart.setVotes(cr.getCount());
				chart.setShare(cr.getShare());
				
				chart.setFullColor(chartColorArr[index%8]);
				chart.setShadowColor(shadowColorArr[index%8]);
				
				chart.setChoiceText(cr.getContent());
				chart.setChoiceTextSize(textSize);
				chart.setChoiceTextColor(chartColorArr[index%8]);
				
				chart.setAnswerType(3);
				chart.setAnswerText(3);
				chart.setAnswerTextColor(chartColorArr[index%8]);
				chart.setAnswerTextSize(textSize);
				chartList.add(chart);
			}
		}
		
		Log.i(LOG_TAG,"question name:"+statistics.getQuestion().getQuestionName());
		
		baseBean.setQuestionText(statistics.getQuestion().getQuestionName());
		baseBean.setCrList(statistics.getPriorityMap().get(1));
		
		Log.i(LOG_TAG,"------------end initialPriorityData-------------");
	}
	
	public void initialGradingData(String dataXML){
		Log.i(LOG_TAG,"------------start initialGradingData-------------");
		Log.i(LOG_TAG,"parse dataXML:"+dataXML);
		
		GradingStatistics statistics =  StatisticsBuilder.parseGradingStatistics(dataXML);
		Log.i(LOG_TAG,statistics.toMessage());
		
		List<ChoiceResult> choiceResultList = statistics.getChoiceResults();
		if(choiceResultList==null||choiceResultList.size()==0){
			return;
		}
		
		String appendTitle = "(SlideIndex:"+statistics.getSlide()+")";
		this.appendTitle(appendTitle);
		Log.i(LOG_TAG,appendTitle);
		
		for(ChoiceResult cr:choiceResultList){
			BaseChart chart = new BaseChart();
			int index = cr.getIndex();
			Log.i(LOG_TAG,"index:"+index);
			chart.setIndex(index);
			chart.setVotes(cr.getCount());
			if(statistics.getTotal()>0){
				chart.setShare(cr.getCount()*1.0/statistics.getTotal());
			}else{
				chart.setShare(0);
			}
			chart.setChoiceText(String.valueOf(cr.getContent()));
			chart.setFullColor(chartColorArr[index%8]);
			chart.setShadowColor(shadowColorArr[index%8]);
			chart.setChoiceTextSize(textSize);
			chart.setChoiceTextColor(chartColorArr[index%8]);
			chart.setAnswerType(3);
			chart.setAnswerText(3);
			chart.setAnswerTextColor(chartColorArr[index%8]);
			chart.setAnswerTextSize(textSize);
			chartList.add(chart);
		}
		
		
		Log.i(LOG_TAG,"question name:"+statistics.getQuestion().getQuestionName());
		
		baseBean.setQuestionText(statistics.getQuestion().getQuestionName());
		baseBean.setCrList(statistics.getChoiceResults());
		
		Log.i(LOG_TAG,"------------end initialGradingData-------------");
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(LOG_TAG,"onCreate");
		
		Bundle bundle = this.getIntent().getExtras();
		String message = bundle.getString("message");
		Log.i(LOG_TAG,message);
		
		type = bundle.getString("type");
		Log.i(LOG_TAG,"type:"+type);
		
		dataXML = message;
//		initialData(message);
		
		if("42".equals(type)){//for priority question
			String priorityStr = bundle.getString("displayPriority");
			if(priorityStr!=null&&priorityStr.length()>0){
				priority = Integer.valueOf(priorityStr);
			}else{
				priority = 1;
			}
			initialPriorityData(message,priority);
		}else if("45".equals(type)){//for priority question
			initialGradingData(message);
		}else{//for other question
			initialData(message);
		}
		
		mGestureDetector = new GestureDetector((OnGestureListener) this); 

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	public void gotoAnswerActivity(Context context,Class answerActivity){
		Log.i(LOG_TAG,"gotoAnswerActivity:"+answerActivity.getName());
		
		Intent intent = new Intent();
	    intent.setClass(context, answerActivity);
		Bundle bundle = new Bundle();
	    bundle.putString("message",dataXML);
	    intent.putExtras(bundle);
	    finish();//finish current activity
	    startActivity(intent);
	}

	
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return mGestureDetector.onTouchEvent(event);
	}


	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	protected int verticalMinDistance = 10;
    protected int minVelocity         = 0;
    protected Integer priority = 1;
	protected Integer prioritySize = 0;
	protected PriorityStatistics statistics = null;
	
	//if user fling left, priority should move front,if user fling right, priority should move back
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
        if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
        	Log.i(LOG_TAG,"Move Left");
        	if(type==null) type = "";
        	Toast.makeText(this.getBaseContext(), "Move Left...type:"+type,Toast.LENGTH_SHORT);
        	
        	//priority question
        	if("42".equals(type)&&priority>1){
        		Log.i(LOG_TAG,"priority:"+priority);
        		Log.i(LOG_TAG,"dataXML:"+dataXML);
        		processMessageForPirority(MessageReceiveService.MSG_SLIDE_RESULT,dataXML,--priority);
        	}
        } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
        	Log.i(LOG_TAG,"Move Right");
        	if(type==null) type = "";
        	Toast.makeText(this.getBaseContext(), "Move Right...type:"+type,Toast.LENGTH_SHORT);
//        	if(priority>1){
//        		Intent intent = new Intent();
//        	    intent.setClass(this.getBaseContext(), PriorityAnswerActivity.class);
//        		Bundle bundle = new Bundle();
//        	    bundle.putString("message",message);
//        	    bundle.putString("priority",String.valueOf(priority-1));
//        	    intent.putExtras(bundle);
//        	    startActivity(intent);        		
//        	}
        	//priority question
        	if("42".equals(type)&&priority<prioritySize){
        		Log.i(LOG_TAG,"priority:"+priority);
        		Log.i(LOG_TAG,"dataXML:"+dataXML);
        		processMessageForPirority(MessageReceiveService.MSG_SLIDE_RESULT,dataXML,++priority);
        	}
        }
		return false;
	}


	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean onDoubleTap(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean onDoubleTapEvent(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean onSingleTapConfirmed(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

}
